//
//  DemandCell.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/10/29.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "DemandCell.h"
#import "HomeModel.h"

//#import "LRAnimationProgress.h"
@interface DemandCell()
@property (weak, nonatomic) IBOutlet UIImageView *iv;
@property (weak, nonatomic) IBOutlet UILabel *contentL;
@property (weak, nonatomic) IBOutlet UIView *animationView;
@property (weak, nonatomic) IBOutlet UILabel *demandL;
@property (weak, nonatomic) IBOutlet UILabel *percentL;
@property (weak, nonatomic) IBOutlet UILabel *nickNamedAndIndustry;
@property (weak, nonatomic) IBOutlet UILabel *creatTimeL;
@property (weak, nonatomic) IBOutlet UILabel *nickNamedL;
@property (weak, nonatomic) IBOutlet UIButton *indicationBtn;
- (IBAction)contactBtn:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIButton *contactBtn;
@property (weak, nonatomic) IBOutlet UIButton *phoneBtn;
@property (weak, nonatomic) IBOutlet UIButton *weiChatBtn;
@property (weak, nonatomic) IBOutlet UIButton *QQBtn;
@property (weak, nonatomic) IBOutlet UIImageView *ivSub;
@property (weak, nonatomic) IBOutlet UILabel *markOneL;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *markOneW;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *markTwoW;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *markThreeW;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *markFourW;
@property (weak, nonatomic) IBOutlet UILabel *markTwoL;
@property (weak, nonatomic) IBOutlet UILabel *markThreeL;
@property (weak, nonatomic) IBOutlet UILabel *markFourL;
@property(nonatomic,strong) UIButton *preBtn;
@property(nonatomic,strong) NSIndexPath *preIndexPath;
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@property(nonatomic,strong) NSString *urlString;
@property(nonatomic,strong) NSDictionary *dict;
@property(nonatomic,strong) CAShapeLayer *lineLayer;
@property (weak, nonatomic) IBOutlet UIButton *collectBtn;
@property(nonatomic,strong) NSString *uids;

@end
static NSString *identifier = @"DemandCell";
@implementation DemandCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.manager = [AFHTTPSessionManager manager];
//    self.uids = [NSString stringWithFormat:@"%@",[UserDefaultsHelper readUid]];
    self.view.layer.masksToBounds = YES;
    self.view.layer.cornerRadius = 3;
    self.view.layer.borderColor = kLineColor.CGColor;
    self.view.layer.borderWidth = 1;
    self.subView.layer.masksToBounds = YES;
    self.subView.layer.cornerRadius = 3;
    self.subView.layer.borderColor = kLineColor.CGColor;
    self.subView.layer.borderWidth = 1;
    self.subView.hidden = YES;
    self.iv.layer.masksToBounds = YES;
    self.iv.layer.cornerRadius = 38/2;
    self.iv.layer.borderColor = kYuanbianColor.CGColor;
    self.iv.layer.borderWidth = 1;
    self.ivSub.layer.masksToBounds = YES;
    self.ivSub.layer.cornerRadius = 38/2;
    self.ivSub.layer.borderColor = kYuanbianColor.CGColor;
    self.ivSub.layer.borderWidth = 1;
    self.contactBtn.layer.masksToBounds = YES;
    self.contactBtn.layer.cornerRadius = 3;
    self.contactBtn.layer.borderColor = [UIColor colorWithRed:187/255.0 green:187/255.0 blue:187/255.0 alpha:1].CGColor;
    self.contactBtn.layer.borderWidth = 1;
    [self.contactBtn setTitleColor:kLabel16Color forState:UIControlStateNormal];
    self.phoneBtn.layer.masksToBounds = YES;
    self.phoneBtn.layer.borderWidth = 1;
    self.phoneBtn.layer.cornerRadius = 4;
    self.weiChatBtn.layer.masksToBounds = YES;
    self.weiChatBtn.layer.cornerRadius = 4;
    self.weiChatBtn.layer.borderWidth = 1;
    self.weiChatBtn.layer.cornerRadius = 4;
    self.weiChatBtn.layer.borderWidth = 1;
    self.QQBtn.layer.masksToBounds = YES;
    self.QQBtn.layer.cornerRadius = 4;
    self.QQBtn.layer.borderColor = kLineColor.CGColor;
    self.QQBtn.layer.borderWidth = 1;
    self.animationView.layer.masksToBounds = YES;
    self.animationView.layer.cornerRadius = 3;
    self.animationView.backgroundColor = kLineColor;
    self.markOneL.layer.cornerRadius = 3;
    self.markOneL.layer.borderColor  = [UIColor colorWithRed:187/255.0 green:187/255.0 blue:187/255.0 alpha:1].CGColor;
    self.markOneL.layer.borderWidth  = 1;
    self.markOneL.textColor = kLabel102Color;
    self.markTwoL.layer.cornerRadius = 3;
    self.markTwoL.layer.borderColor  = [UIColor colorWithRed:187/255.0 green:187/255.0 blue:187/255.0 alpha:1].CGColor;
    self.markTwoL.layer.borderWidth  = 1;
    self.markTwoL.textColor = kLabel102Color;
    self.markThreeL.layer.cornerRadius = 3;
    self.markThreeL.layer.borderColor  = [UIColor colorWithRed:187/255.0 green:187/255.0 blue:187/255.0 alpha:1].CGColor;
    self.markThreeL.layer.borderWidth  = 1;
    self.markThreeL.textColor = kLabel102Color;
    self.markFourL.layer.cornerRadius = 3;
    self.markFourL.layer.borderColor  = [UIColor colorWithRed:187/255.0 green:187/255.0 blue:187/255.0 alpha:1].CGColor;
    self.markFourL.layer.borderWidth  = 1;
    self.markFourL.textColor = kLabel102Color;
    
}
+(instancetype)cellNibWithTableView:(UITableView *)tableView
{
    DemandCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        [tableView registerNib:[UINib nibWithNibName:@"DemandCell" bundle:nil] forCellReuseIdentifier:identifier];
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)setHomeModel:(HomeModel *)homeModel
{
    _homeModel = homeModel;
    [self.lineLayer removeFromSuperlayer];
//    homeModel.showView = homeModel.showView;
    if (homeModel.show) {
        self.view.hidden = YES;
        self.subView.hidden = NO;
    }else {
        self.view.hidden = NO;
        self.subView.hidden = YES;
    }
    XLog(@"homeModel.collectStatus:%ld",homeModel.collectStatus);
    if (homeModel.collectStatus ==1) {
        self.collectBtn.selected = YES;
    }else {
        self.collectBtn.selected = NO;
    }
    NSString *string = @"";
    if (homeModel.nicakName.length) {
        string = [NSString stringWithFormat:@"%@|%@",homeModel.nicakName,homeModel.industryList[0]];
    }else {
        string = [NSString stringWithFormat:@"%@|%@",homeModel.phone,homeModel.industryList[0]];
    }
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:string];
    NSString *subString = [NSString stringWithFormat:@"|%@",homeModel.industryList[0]];
    [text addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:105/255.0 green:105/255.0 blue:105/255.0 alpha:1]range:[string rangeOfString:subString]];
    self.nickNamedAndIndustry.attributedText = text;
    [self.iv sd_setImageWithURL:[NSURL URLWithString:homeModel.headImg] placeholderImage:[UIImage imageNamed:@"headImg"]];
    NSTimeInterval interval    = homeModel.creattime/1000.0;
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *currentDateStr = [formatter stringFromDate: date];
    self.creatTimeL.text = currentDateStr;
    self.contentL.text = [NSString stringWithFormat:@"需求概述:%@",homeModel.title];
    XLog(@"homeModel.represent:%@",homeModel.represent);
    XLog(@"homemodel:%@",homeModel.associationtypesubList);
    XLog(@"homeModel.associationtypesubList.count:%ld",homeModel.associationtypesubList.count);
    [self.ivSub sd_setImageWithURL:[NSURL URLWithString:homeModel.headImg] placeholderImage:[UIImage imageNamed:@"headImg"]];
    self.nickNamedL.text = homeModel.nicakName;
    XLog(@"self.nickNamedL.text:%@",self.nickNamedL.text);
    if (homeModel.phone.length == 0) {
        [self.phoneBtn setTitleColor:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1] forState:UIControlStateNormal];
        self.phoneBtn.layer.borderColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1].CGColor;
        [self.phoneBtn setTitle:[NSString stringWithFormat:@" 电话:该客户暂未填写"] forState:UIControlStateNormal];
    }else{
        [self.phoneBtn setTitle:[NSString stringWithFormat:@" 电话:%@",homeModel.phone] forState:UIControlStateNormal];
        self.phoneBtn.layer.borderColor = [UIColor colorWithRed:242/255.0 green:176/255.0 blue:81/255.0 alpha:1].CGColor;
        
        
    }
    if (homeModel.wechat.length == 0) {
        [self.weiChatBtn setTitleColor:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1] forState:UIControlStateNormal];
        self.weiChatBtn.layer.borderColor = [UIColor colorWithRed:224/255.0 green:224/255.0 blue:224/255.0 alpha:0.5].CGColor;
        [self.weiChatBtn setTitle:[NSString stringWithFormat:@" 微信:该客户暂未填写"] forState:UIControlStateNormal];
    }else{
        [self.weiChatBtn setTitle:[NSString stringWithFormat:@" 微信:%@",homeModel.wechat] forState:UIControlStateNormal];
        self.weiChatBtn.layer.borderColor = [UIColor colorWithRed:151/255.0 green:193/255.0 blue:91/255.0 alpha:1].CGColor;
    }
    if (homeModel.qq.length == 0) {
        [self.QQBtn setTitleColor:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1] forState:UIControlStateNormal];
        self.QQBtn.layer.borderColor = [UIColor colorWithRed:224/255.0 green:224/255.0 blue:224/255.0 alpha:0.5].CGColor;
        [self.QQBtn setTitle:[NSString stringWithFormat:@" QQ:该客户暂未填写"] forState:UIControlStateNormal];
    }else{
        [self.QQBtn setTitle:[NSString stringWithFormat:@" QQ:%@",homeModel.qq] forState:UIControlStateNormal];
        self.QQBtn.layer.borderColor = [UIColor colorWithRed:194/255.0 green:194/255.0 blue:194/255.0 alpha:1].CGColor;
        
    }
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:CGPointMake(0, 3)];
        path.lineCapStyle = kCGLineCapRound;
        [path addLineToPoint:CGPointMake(homeModel.believe*53, 3)];
        self.percentL.text = [NSString stringWithFormat:@"%@%%",[self formatFloat:homeModel.believe*100]];
        if (homeModel.believe*100 > 0) {
            self.markOneL.hidden = YES;
            self.markTwoL.hidden = YES;
            self.markThreeL.hidden = YES;
            self.markFourL.hidden = YES;
            self.percentL.hidden = NO;
            self.animationView.hidden = NO;
            self.demandL.text = @"需求可信度:";
        }else {
            self.percentL.hidden = YES;
            self.animationView.hidden = YES;
            self.demandL.text = @"需求类型:";
            if (homeModel.associationtypesubList.count) {
                if (homeModel.associationtypesubList.count ==1) {
                    self.markOneL.text = homeModel.associationtypesubList[0];
                    self.markOneW.constant = [MCLabel getWidthWithTitle:homeModel.associationtypesubList[0] withFontSize:12]+10;
                    CGFloat markX = 94+self.markOneW.constant;
                    CGFloat collectBtnX = CGRectGetMinX(self.collectBtn.frame);//305
                    if (markX>collectBtnX) {
                        self.markOneL.hidden = YES;
                    }else {
                        self.markOneL.hidden = NO;
                    }
//                    self.markOneL.hidden = NO;
                    self.markTwoL.hidden = YES;
                    self.markThreeL.hidden = YES;
                    self.markFourL.hidden = YES;
                }else if (homeModel.associationtypesubList.count ==2) {
                    self.markOneL.text = homeModel.associationtypesubList[0];
                    self.markOneW.constant = [MCLabel getWidthWithTitle:homeModel.associationtypesubList[0] withFontSize:12]+10;
                    self.markTwoL.text = homeModel.associationtypesubList[1];
                    self.markTwoW.constant = [MCLabel getWidthWithTitle:homeModel.associationtypesubList[1] withFontSize:12]+10;
                    self.markOneL.hidden = NO;
                    CGFloat markX = 94+self.markOneW.constant+self.markTwoW.constant;
                    CGFloat collectBtnX = CGRectGetMinX(self.collectBtn.frame);//305
                    if (markX>collectBtnX) {
                        self.markTwoL.hidden = YES;
                    }else {
                        self.markTwoL.hidden = NO;
                    }
//                    self.markTwoL.hidden = NO;
                    self.markThreeL.hidden = YES;
                    self.markFourL.hidden = YES;
                }else if (homeModel.associationtypesubList.count ==3) {
                    self.markOneL.text = homeModel.associationtypesubList[0];
                    self.markOneW.constant = [MCLabel getWidthWithTitle:homeModel.associationtypesubList[0] withFontSize:12]+10;
                    self.markTwoL.text = homeModel.associationtypesubList[1];
                    self.markTwoW.constant = [MCLabel getWidthWithTitle:homeModel.associationtypesubList[1] withFontSize:12]+10;
                    self.markThreeL.text = homeModel.associationtypesubList[2];
                    self.markThreeW.constant = [MCLabel getWidthWithTitle:homeModel.associationtypesubList[2] withFontSize:12]+10;
                    self.markOneL.hidden = NO;
                    self.markTwoL.hidden = NO;
                    CGFloat markX = 94+self.markOneW.constant+self.markTwoW.constant+self.markThreeW.constant;
                    CGFloat collectBtnX = CGRectGetMinX(self.collectBtn.frame);//305
                    if (markX>collectBtnX) {
                        self.markThreeL.hidden = YES;
                    }else {
                        self.markThreeL.hidden = NO;
                    }
//                    self.markThreeL.hidden = NO;
                    self.markFourL.hidden = YES;
                }else if (homeModel.associationtypesubList.count >=4) {
                    self.markOneL.text = homeModel.associationtypesubList[0];
                    self.markOneW.constant = [MCLabel getWidthWithTitle:homeModel.associationtypesubList[0] withFontSize:12]+10;
                    self.markTwoL.text = homeModel.associationtypesubList[1];
                    self.markTwoW.constant = [MCLabel getWidthWithTitle:homeModel.associationtypesubList[1] withFontSize:12]+10;
                    self.markThreeL.text = homeModel.associationtypesubList[2];
                    self.markThreeW.constant = [MCLabel getWidthWithTitle:homeModel.associationtypesubList[2] withFontSize:12]+10;
                    self.markFourL.text = homeModel.associationtypesubList[3];
                    self.markFourW.constant = [MCLabel getWidthWithTitle:homeModel.associationtypesubList[3] withFontSize:12]+10;
                    self.markOneL.hidden = NO;
                    self.markTwoL.hidden = NO;
                    self.markThreeL.hidden = NO;
//                    self.markFourL.hidden = NO;
                    CGFloat markFourX = 94+self.markOneW.constant+self.markThreeW.constant+self.markThreeW.constant+self.markFourW.constant+30;//288
                    CGFloat collectBtnX = CGRectGetMinX(self.collectBtn.frame);//305
                    if (markFourX>collectBtnX) {
                        self.markFourL.hidden = YES;
                    }else {
                        self.markFourL.hidden = NO;
                    }
                }
            }else {
                self.markOneL.hidden = YES;
                self.markTwoL.hidden = YES;
                self.markThreeL.hidden = YES;
                self.markFourL.hidden = YES;
                
            }
        }

        path.lineWidth = 6;
        self.lineLayer = [CAShapeLayer layer];
        self.lineLayer.lineWidth = 6;
        self.lineLayer.lineCap = kCALineCapRound;
        self.lineLayer.strokeColor = [UIColor colorWithRed:151/255.0 green:193/255.0 blue:91/255.0 alpha:1].CGColor;
        self.lineLayer.path = path.CGPath;
        [self.animationView.layer addSublayer:self.lineLayer];
    
}
- (void)setCollectShow:(BOOL)collectShow
{
    _collectShow = collectShow;
    if (collectShow) {
        self.collectBtn.hidden = NO;
    }else {
        self.collectBtn.hidden = YES;
    }
}
- (IBAction)contactBtn:(UIButton *)sender {

    sender.selected = YES;
    self.indicationBtn.selected = NO;
//    self.homeModel.show = YES;
//    self.view.hidden = YES;
//    self.subView.hidden = NO;
    XLog(@"sender:%d",sender.selected);
    
    if ([self.delegate respondsToSelector:@selector(demandCell:withClickBtn:withIndexPath:withHomeModel:)]) {
        [self.delegate demandCell:self withClickBtn:sender withIndexPath:self.indexPath withHomeModel:self.homeModel];
    }
//    if (self.indexPath.row) {
//        <#statements#>
//    }
//    sender.selected = NO;
}
- (IBAction)indicatorBtn:(UIButton *)sender {

    sender.selected = NO;
//    self.view.hidden = NO;
//    self.subView.hidden = YES;
//    self.homeModel.show = NO;
//    sender.selected = NO;
    self.contactBtn.selected = NO;
    if ([self.delegate respondsToSelector:@selector(demandCell:withClickBtn:withIndexPath:withHomeModel:)]) {
        [self.delegate demandCell:self withClickBtn:sender withIndexPath:self.indexPath withHomeModel:self.homeModel];
    }
}
#pragma mark-代理:收藏
- (IBAction)collectBtn:(UIButton *)sender
{
//    sender.selected = !sender.selected;
//    self.homeModel.collectStatus = sender.selected;
    if ([self.delegate respondsToSelector:@selector(demandCell:withCollectBtn:withIndexPath:withHomeModel:)]) {
        [self.delegate demandCell:self withCollectBtn:sender withIndexPath:self.indexPath withHomeModel:self.homeModel];
    }
    
    
}


//-(void)dealloc
//{
//    [[NSNotificationCenter defaultCenter]removeObserver:self];
//}
- (void)setIndexPath:(NSIndexPath *)indexPath
{
    _indexPath = indexPath;
}
- (IBAction)phoneBtn:(UIButton *)sender {
    if ([sender currentTitle].length) {
        NSMutableString * callPhone= [[NSMutableString alloc] initWithFormat:@"tel:%@",self.homeModel.phone];
        if (@available(iOS 10.0, *)) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone] options:@{} completionHandler:nil];
        } else {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
        }
        self.urlString = [NSString stringWithFormat:@"%@/relation/insertRelation",kUrl];
        NSString *ids = [NSString stringWithFormat:@"%ld",self.homeModel.id];
        self.dict = @{
                      @"uid":[UserDefaultsHelper readUid],
                      @"relationId":ids,
                      @"type":@"1",
                      @"token":[UserDefaultsHelper readToken]
                      };
        XLog(@"self.dict:%@",self.dict);
        [self.manager POST:self.urlString parameters:self.dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            XLog(@"responseObject:%@",responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            XLog(@"error:%@",error);
        }];
        
    }else {
        [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"该客户暂未填写手机号,无法联系"];
    }
    
}
- (IBAction)weiChatBtn:(UIButton *)sender {
    
    if (self.homeModel.wechat.length != 0) {
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string = [sender currentTitle];
        NSURL *url = [NSURL URLWithString:@"weixin://"];
        BOOL canOpen = [[UIApplication sharedApplication] canOpenURL:url];
        if (canOpen) {
            [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"微信号已复制到剪切板"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [[UIApplication sharedApplication] openURL:url];
                
            });
        }else{
            [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"请安装微信"];
        }
        
    }else {
        [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"该客户暂未填写微信号,无法联系"];
    }
    
}
- (IBAction)qqBtn:(UIButton *)sender {

     if (self.homeModel.qq.length != 0) {
         if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"mqq://"]]) {
             UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
             NSURL * url=[NSURL URLWithString:[NSString stringWithFormat:@"mqq://im/chat?chat_type=wpa&uin=%@&version=1&src_type=web",self.homeModel.qq]];
             NSURLRequest *request = [NSURLRequest requestWithURL:url];
             [webView loadRequest:request];
             [self.view addSubview:webView];
         }else{
             XLog(@"对不起，您还未安装QQ");
             [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"请安装QQ"];
         }
         
     }else {
         [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"该客户暂未填写QQ号,无法联系"];
     }

}
- (NSString *)formatFloat:(float)f
{
    if (fmodf(f, 1)==0) {//如果有一位小数点
        return [NSString stringWithFormat:@"%.0f",f];
    } else if (fmodf(f*10, 1)==0) {//如果有两位小数点
        return [NSString stringWithFormat:@"%.1f",f];
    } else {
        return [NSString stringWithFormat:@"%.2f",f];
    }
}

@end
